package com.idrl.wap.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.idrl.Const;
import com.idrl.dao.po.User;
import com.idrl.web.action.BaseAction;


public class WapAllAction extends BaseAction {

	private static Logger log = Logger.getLogger(WapAllAction.class);	
	
	private long uid;
	private String key;	
	
	public String getLoginKey()
	{
		User user = this.getUserFromSession();
		uid = user.getId();
		key = com.ambow.core.util.MD5.crypt(Const.MD5_PREFIX + user.getEmail() + user.getPassword());
		return "loginByKey";
	}
	
	/**
	 * 通过登录码登录
	 * @return
	 */
	public String loginByKey()
	{
		if(log.isDebugEnabled())
		{
			log.debug("uid=" + uid);
			log.debug("key=" + key);
		}
		
		if(uid == 0 || StringUtils.isBlank(key))
		{
			this.getRequest().setAttribute(Const.KEY_INFO_MSG,	"非法的登录码!");
			log.error("uid=" + uid);
			log.error("key=" + key);
			return LOGIN;
		}
		
		//是否已登录
		User user = this.getUserFromSession();		
		if (user == null || user.getId() != uid)
		{
			user = this.userService.get(uid);
		}
		
		if(user == null)
		{
			this.getRequest().setAttribute(Const.KEY_INFO_MSG,	"非法的登录码!");
			log.error("uid=" + uid);
			log.error("key=" + key);
		}
		else
		{		
			if(user.getState() == User.INVALID)
			{
				this.getRequest().setAttribute(Const.KEY_INFO_MSG,	"此邮箱未激活！");
			}
			else if(user.getState() == User.TEMP)
			{
				this.getRequest().setAttribute(Const.KEY_INFO_MSG,  "Sorry，临时用户不能登录!");
			} 			
			else if ( key.equals(com.ambow.core.util.MD5.crypt(Const.MD5_PREFIX + user.getEmail() + user.getPassword())))
			{	
				this.setUserToSession(user);				
				return HOME;
			}
			else
			{
				this.getRequest().setAttribute(Const.KEY_INFO_MSG,   "非法的激活码!");			
			}
		}
		return LOGIN;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public long getUid() {
		return uid;
	}

	public void setUid(long uid) {
		this.uid = uid;
	}
	
	
	
}
